package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import dto.LogonDTO;
import dto.UserDTO;

public class AddUserPage extends JDialog {

	private static final long serialVersionUID = 1L;

	private JComboBox comboBoxLevel;
	private JLabel labelMessage;

	private JTextField textFieldName;
	private JTextField textFieldUsername;
	private JTextField textFieldPassword;
	private JTextField textFieldEditorUsername;
	private JTextField textFieldEditorLevel;

	JButton btnEnter = new JButton("Enter");
	JButton btnCancel = new JButton("Cancel");

	private UserDTO userDTO;

	public UserDTO getUserDTO() {
		userDTO.setUserName(textFieldName.getText().trim());
		userDTO.setUserId(textFieldUsername.getText().trim());
		userDTO.setPassword(textFieldPassword.getText().trim());
		userDTO.setAccessLevel(comboIndexToLevel(comboBoxLevel
				.getSelectedIndex()));

		return userDTO;
	}

	/**
	 * Create the dialog.
	 */
	public AddUserPage(LogonDTO logonDTO) {

		userDTO = new UserDTO();

		setBounds(100, 100, 571, 468);
		// setBounds(100, 100, 450, 300);
		getContentPane().setLayout(
				new MigLayout("", "[grow][grow][][grow][]",
						"[][][][][][][][][][][]"));
		{
			JLabel lblJaLibrary = new JLabel("J&A Library");
			lblJaLibrary.setForeground(Color.BLUE);
			lblJaLibrary.setFont(new Font("Arial", Font.BOLD, 16));
			getContentPane().add(lblJaLibrary, "cell 0 0 5 1,alignx center");
		}
		{
			JLabel lblAddUser = new JLabel("Add User");
			getContentPane().add(lblAddUser, "cell 0 1 5 1,alignx center");
		}
		{
			JLabel label = new JLabel("     ");
			getContentPane().add(label, "cell 0 2");
		}
		{
			JLabel lblName = new JLabel("Name:");
			getContentPane().add(lblName, "cell 0 3,alignx trailing");
		}
		{
			textFieldName = new JTextField();
			getContentPane().add(textFieldName, "cell 1 3 3 1,growx");
			textFieldName.setColumns(10);
		}
		{
			JLabel lblUsername = new JLabel("Username:");
			getContentPane().add(lblUsername, "cell 0 4,alignx trailing");
		}
		{
			textFieldUsername = new JTextField();
			getContentPane().add(textFieldUsername, "cell 1 4,growx");
			textFieldUsername.setColumns(10);
		}
		{
			JLabel lblPassword = new JLabel("Password:");
			getContentPane().add(lblPassword, "cell 0 5,alignx trailing");
		}
		{
			textFieldPassword = new JTextField();
			getContentPane().add(textFieldPassword, "cell 1 5,growx");
			textFieldPassword.setColumns(10);
		}
		{
			JLabel lblAccessLevel = new JLabel("Access Level:");
			getContentPane().add(lblAccessLevel, "cell 0 6,alignx trailing");
		}
		{
			comboBoxLevel = new JComboBox();
			comboBoxLevel.setModel(new DefaultComboBoxModel(new String[] {
					"Member", "Librarian", "Administrator" }));
			comboBoxLevel.setSelectedIndex(0);
			getContentPane().add(comboBoxLevel, "cell 1 6,growx");
		}

		// ENTER button
		getContentPane().add(btnEnter, "cell 0 8,growx");
		{
			// CANCEL button
			btnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					dispose();
				}
			});
			getContentPane().add(btnCancel, "cell 4 8");
		}
		{
			labelMessage = new JLabel("     ");
			labelMessage.setForeground(Color.BLUE);
			labelMessage.setFont(new Font("Arial", Font.BOLD, 12));
			getContentPane().add(labelMessage, "cell 0 9 5 1");
		}
		{
			textFieldEditorUsername = new JTextField();
			textFieldEditorUsername.setEditable(false);
			textFieldEditorUsername.setText(logonDTO.getUserName());
			getContentPane()
					.add(textFieldEditorUsername, "cell 0 10 2 1,growx");
			textFieldEditorUsername.setColumns(10);
		}
		{
			textFieldEditorLevel = new JTextField();
			textFieldEditorLevel.setEditable(false);
			textFieldEditorLevel.setText(logonDTO.getUserName());
			getContentPane().add(textFieldEditorLevel, "cell 3 10 2 1,growx");
			textFieldEditorLevel.setColumns(10);
		}
	}

	private int comboIndexToLevel(int index) {
		switch (index) {
		case 0:
			return 3;
		case 1:
			return 2;
		case 2:
			return 1;
			// case 3:
			// return 0;
		}
		// return -1;
		return 0;
	}

	public void clearFields() {
		textFieldName.setText("");
		textFieldUsername.setText("");
		textFieldPassword.setText("");
		comboBoxLevel.setSelectedIndex(0);
	}

	public void addAddUserButtonListener(ActionListener al) {
		btnEnter.addActionListener(al);
	}

	public void displayMessage(String message) {
		labelMessage.setText(message);
	}

}
